﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WebSocket4Net;

namespace WebSocket4Net_Client_Demo
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            txtUserName.Text = new Random().Next(1000).ToString();
        }

        //有关 websocket4net：https://archive.codeplex.com/?p=websocket4net
        private WebSocket ws = null;
        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtUserName.Text))
            {
                return;
            }
            ws = new WebSocket(txtConnectAddress.Text.Trim() + "?userName=" + txtUserName.Text.Trim());
            ws.Opened += Ws_Opened;
            ws.MessageReceived += Ws_MessageReceived;
            ws.DataReceived += Ws_DataReceived;
            ws.Error += Ws_Error;          
            ws.Closed += Ws_Closed;

            try
            {
                ws.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            
        }

        private void Ws_DataReceived(object sender, DataReceivedEventArgs e)
        {
            
        }

        private void Ws_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            txtChaRecord.AppendText(e.Message + "\r\n");
        }

        private void Ws_Error(object sender, SuperSocket.ClientEngine.ErrorEventArgs e)
        {
            MessageBox.Show(e.Exception.ToString());
        }

        private void Ws_Opened(object sender, EventArgs e)
        {
            lblConnectState.Text = "连接已打开";
            lblConnectState.ForeColor = Color.Green;
        }

        private void Ws_Closed(object sender, EventArgs e)
        {
            lblConnectState.Text = "连接已关闭";
            lblConnectState.ForeColor = Color.Red;
        }

       
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtSendContent.Text))
            {
                if (ws.State== WebSocketState.Closed || ws.State== WebSocketState.Closing)
                {
                    try
                    {
                        ws.Open();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }

                ws.Send(txtSendContent.Text.Trim());
                txtSendContent.Text = string.Empty;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (ws != null && ws.State == WebSocketState.Open)
            {
                ws.Close();
            }
        }
    }
}
